/**
  * Triangulated surface with fixed tesellation direction.
  */

#pragma once
#include "Cell.h"

class TriCell :
    public Cell
{
private:
    float minHeight, maxHeight;

    //true normals
    Vector3f n0, n1;

    //graphical normals
    Vector3f n_s00, n_s10, n_s01, n_s11;

    float x, y;
    float z00, z10, z01, z11;
    float dx0, dx1, dy0, dy1;

    Vector3f n_s(float relx, float rely) const;

    Intersection traceNegative(Ray& ray) const;
    Intersection tracePositive(Ray& ray) const;

    Intersection traceNegativeSlope(Ray& ray) const;
    Intersection tracePositiveSlope(Ray& ray) const;
public:
    //Returns the minimum height of this cell.
    float getMaxHeight() const;

    //Returns the maximum height of this cell.
    float getMinHeight() const;

    //Traces a ray into this cell. Returns the intersection.
    Intersection traceRay(Ray& ray) const;

    Intersection traceSlopeRay(Ray& slopeRay) const;

    //Traces a shadow ray into this cell. Returns true iff there is a collision.
    bool traceShadowRay(Ray& ray) const;

    void setFromHeightmap(Heightmap& heightmap, int x, int y);
};
